package com.order.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.order.bean.Foods;
import com.order.bean.Order;
import com.order.bean.OrderDetail;
import com.order.bean.OrderDto;
import com.order.service.OrderService;
import com.order.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "OrderController", value = "/order/*")
public class OrderController extends BaseController {

    private OrderService orderService = new OrderServiceImpl();

    protected void updateOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        Order order = JSON.parseObject(json, Order.class);
        Order updated = orderService.updateOrder(order);
        this.writeResultAndData(response, updated);
    }

    protected void checkOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        Order order = JSON.parseObject(json, Order.class);
        Order checked = orderService.checkOutOrder(order);
        this.writeResultAndData(response, checked);
    }

    protected void batchInsertDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        OrderDto orderDto = JSON.parseObject(json, OrderDto.class);
        // 将OrderDto中的selectedFoods属性转换为OrderDetail列表
        List<OrderDetail> orderDetails = JSONUtil.toList(orderDto.getSelectedFoods(), OrderDetail.class);
        // 遍历每个订单详情
        orderDetails.forEach(item->{
            item.setOrderId(orderDto.getOrderId());// 设置订单ID
            item.setUserId(orderDto.getUserId());// 设置用户ID
        });
        orderService.batchInsertDetail(orderDetails);
        this.writeResult(response, true);
    }

    protected void getUserNewestOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        Order order = JSON.parseObject(json, Order.class);
        Order userNewOrder = orderService.getUserNewOrder(order);
        this.writeResultAndData(response, userNewOrder);

    }

    protected void selectPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        PageInfo<Foods> foodsPageInfo = orderService.selectPage(userId, pageNum, pageSize);
        this.writeResultAndData(response, foodsPageInfo);
    }

}
